package com.carleasoft.mps.adu.business.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carleasoft.mps.adu.business.org.bean.entity.OrganizationType;
import com.carleasoft.mps.adu.business.user.bean.entity.AuthorityUserDept;
import com.carleasoft.mps.adu.business.user.bean.po.UserQueryPO;
import com.carleasoft.mps.adu.business.user.bean.vo.AuthorityUserDetailVo;
import com.carleasoft.mps.adu.business.user.dao.AuthorityUserDeptDao;
import com.carleasoft.mps.adu.business.user.service.AuthorityUserDeptService;
import com.carleasoft.mps.core.user.Constants;
import com.carleasoft.mps.core.user.SortPo;
import com.carleasoft.mps.core.utils.IBeanUtil;
import com.carleasoft.mps.security.permission.data.RequiresDataPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 部门信息表 服务实现类
 * </p>
 *
 * @author ztm
 * @since 2021-09-09
 */
@Service
public class AuthorityUserDeptServiceImpl extends ServiceImpl<AuthorityUserDeptDao, AuthorityUserDept> implements AuthorityUserDeptService {
 @Autowired
 private AuthorityUserDeptDao authorityUserDeptDao;
    /**
     * 重排序
     *
     * @param sortPo
     * @return
     */
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public Boolean updateSort(SortPo sortPo) {
        boolean result = false;
//        if (sortPo.getSort() == null) {
//            //自己
//            AuthorityUserDept own = this.getById(sortPo.getOwnCode());
//            Integer ownSort = own.getSort();
//            Long ownSortDate = own.getSortDate();
//
//            AuthorityUserDept down = this.getById(sortPo.getMoveCode());
//            Integer sort = down.getSort();
//            Long sortDate = down.getSortDate();
//
//            own.setSort(sort);
//            own.setSortDate(sortDate);
//            result = this.updateById(own);
//            down.setSort(ownSort);
//            down.setSortDate(ownSortDate);
//            result = this.updateById(down);
//        } else {
//            LambdaUpdateWrapper<AuthorityUserDept> wrapper = Wrappers.<AuthorityUserDept>lambdaUpdate();
//            wrapper.eq(AuthorityUserDept::getId, sortPo.getOwnCode());
//            wrapper.set(AuthorityUserDept::getSort, sortPo.getSort());
//            result = this.update(wrapper);
//        }
        return result;
    }

    /**
     * 获取信息列表,分页
     *
     * @param page,authorityUserAllInfoVo
     * @return List
     */
    @Override
    @RequiresDataPermission(dataPerm = {@RequiresDataPermission.Scope(method = "getByDept",scope = Constants.DATA_SCOPE_DEPT)})
    public Page pageQuery(Page page, UserQueryPO userQueryPO) {
        return this.authorityUserDeptDao.getBydeptInfo(page,userQueryPO,Constants.SUPER_ADMIN);
    }

    /**
     * 根据部门查询用户信息(全部)
     *
     * @param userQueryPO
     * @return
     */
    @Override
    public List<AuthorityUserDetailVo> getByDept(UserQueryPO userQueryPO) {
        IBeanUtil.paramVerify(userQueryPO,"deptCode");
        Page page = new Page();
        page.setSize(10000L);
        return this.authorityUserDeptDao.getByDept(page,userQueryPO, Constants.SUPER_ADMIN).getRecords();
    }

}
